package com.dirk.happie.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import com.dirk.happie.R;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Keys;

public class GetHistoryTask extends AsyncTask<String, Void, String> {
	private ProgressDialog pDialog;
	private RequestCompleteListener<String> listener = null;
	private App mApp;

	public GetHistoryTask() {
	}

	public GetHistoryTask(Activity context) {
		pDialog = new ProgressDialog(context);
		pDialog.setCancelable(true);
		pDialog.setMessage(context.getString(R.string.gen_loading));
		pDialog.setTitle(context.getString(R.string.gen_please_wait));
		mApp = (App) context.getApplication();
	}

	public void setRequestCompleteListener(RequestCompleteListener<String> listener) {
		this.listener = listener;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub

		pDialog.show();

		super.onPreExecute();
	}

	/*
	 * Do all the network IO and time-consuming parsing in here, this method
	 * will be invoked in its own thread, not blocking the UI.
	 */
	@Override
	protected String doInBackground(String... params) {

		HttpClient client = new DefaultHttpClient();
		String result = null;
		try {

			HttpPost request = new HttpPost(Keys.GET_HISTORY_URL);
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

			nameValuePairs.add(new BasicNameValuePair("user", params[0]));
			nameValuePairs.add(new BasicNameValuePair("_lang", mApp.getLanguage()));

			try {
				request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			} catch (UnsupportedEncodingException e1) {
				// TODO Please log this
			}

			HttpResponse response = client.execute(request);

			HttpEntity entity = response.getEntity();
			// If the response does not enclose an entity, there is no need
			// to worry about connection release

			if (entity != null) {

				// A Simple JSON Response Read
				InputStream instream = null;
				try {
					instream = entity.getContent();

					result = NetworkManager.convertStreamToString(instream);

					Log.i("result history", result);

				} finally {
					// Closing the input stream will trigger connection release
					try {
						instream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			return result;
		} catch (IOException ex) {
			// properly log your exception, don't just printStackTrace()
			// as you can't pinpoint the location and it might be cut off
			Log.e("LOGIN", "failed to log in", ex);
		} finally {
			client.getConnectionManager().shutdown();
		}
		return result;

	}

	/*
	 * This again runs on the UI thread, so only do here what really needs to
	 * run on the UI thread.
	 */
	@Override
	protected void onPostExecute(String response) {
		if (listener != null) {
			listener.onSuccess(response);
		}
		pDialog.dismiss();
	}
}